import Taro from '@tarojs/taro'
import { HTTP_STATUS } from '../const/status'
import { base } from './config'

const token = ''

export default{
  baseOptions(pageObj, params, method = 'GET') {
    let { url, data } = params
    // let token = getApp().globalData.token
    // if (!token) login()
    console.log('params', params)
    let contentType = 'application/x-www-form-urlencoded'
    contentType = params.contentType || contentType
    const option = {
      isShowLoading: false,
      loadingText: '正在加载',
      url: base + url,
      data: data,
      method: method,
      header: { 'content-type': contentType, 'token': token },
      success(res) {
        if (res.statusCode === HTTP_STATUS.SUCCESS) {
          return res.data
        } else{
          return ''
        }
      },
      error(e) {
      }
    }
    return Taro.request(option).then(res => {if(pageObj){pageObj.setState(res.data)}})
  },
  get(pageObj, url, data = '') {
    let option = { url, data }
    return this.baseOptions(pageObj, option)
  },
  post: function (pageObj, url, data, contentType) {
    let params = { url, data, contentType }
    return this.baseOptions(pageObj, params, 'POST')
  }
}
